﻿using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using Tools;

namespace Admin.业务界面
{
    public partial class 导师数据 : Form
    {
        List<Teacher> all_tea;
        int pagenumber = 1;
        int pageSize = 20;
        int totalCount = 0;
        int pageCount = 0;
        bool changed_one;
        public 导师数据()
        {
            InitializeComponent();
        }

        private void 导师数据_Load(object sender, EventArgs e)//加载页面
        {
            pagenumber = 1;
            getPage(ref totalCount);
        }

        private void button5_Click(object sender, EventArgs e)//导入导师数据
        {
            if (openFileDialog1.ShowDialog() != DialogResult.OK)
                return;

            string Path = openFileDialog1.FileName;
            DataTable dt = Tools.Read_excel.read_excel(Path);
            if (dt == null) return;

            List<Teacher> teachers = new List<Teacher>();

            foreach (DataRow dr in dt.Rows)
            {
                Teacher tea = new Teacher();
                tea.teacher_name = dr["导师姓名"].ToString();
                tea.teacher_uname = dr["导师姓名简写"].ToString();
                tea.pwd = "123";
                tea.depart = dr["导师院系"].ToString();
                tea.info = dr["导师简介"].ToString();
                teachers.Add(tea);
            }
            BLL.B_Admin b_tea = new BLL.B_Admin();
            try
            {                
                var res = b_tea.Add_T(teachers);
                pagenumber = 1;
                getPage(ref totalCount);
            }
            catch (Add_Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void button9_Click(object sender, EventArgs e)//删除导师数据
        {
            List<Teacher> tea_deleted = new List<Teacher>();
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                tea_deleted.Add(row.DataBoundItem as Teacher);
            }
            BLL.B_Admin b_Teacher = new BLL.B_Admin();
            try
            {
                if (b_Teacher.Del_T(tea_deleted))
                    MessageBox.Show("删除成功！");
                getPage(ref totalCount);
            }
            catch (Tools.Del_Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void button8_Click(object sender, EventArgs e)//保存修改
        {
            BLL.B_Admin b_Teacher = new BLL.B_Admin();
            try
            {
                if (b_Teacher.Save_List_T(all_tea))
                    MessageBox.Show("保存成功！");
            }
            catch (Tools.Modify_Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        void getPage(ref int totalCount)//换页功能
        {
            BLL.B_Admin b_Teacher = new BLL.B_Admin();
            all_tea = b_Teacher.GetPage_T(pagenumber, pageSize, ref totalCount);
            dataGridView1.DataSource = new BindingList<Teacher>(all_tea);
            pageCount = Convert.ToInt32(Math.Ceiling((double)totalCount / pageSize));
        }

        private void 导师数据_FormClosed(object sender, FormClosedEventArgs e)
        {
            //this.Hide();
            //管理员主界面 f1 = new 管理员主界面();
            //f1.ShowDialog();
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != 3) return;
            changed_one = true;
        }

        private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != 3) return;
            if (changed_one)
            {
                dataGridView1.CurrentCell.Value = MD5Provider.Hash(dataGridView1.CurrentCell.Value.ToString());
                changed_one = false;
            }
        }
    }
}
